package com.example.high_widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.high_widget.R;
import com.example.high_widget.entity.Planet;

import java.util.List;

//继承自己的Adapter，并设置泛型为自己的MyViewHolder
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyViewHolder> {

    private List<Planet> planetList;
    private Context context;



    public RecyclerviewAdapter(Context context,List<Planet> planetList) {
        this.context = context;
        this.planetList = planetList;
    }

    /**
     *获取单体布局，填入MyViewHolder中返回
     */
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(this.context).inflate(R.layout.item_reclyerview,null);

        return new MyViewHolder(itemView);
    }

    /**
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        // 给控件设置好数据
        Planet planet = planetList.get(position);
        holder.iv_icon.setImageResource(planet.image);
        holder.tv_name.setText(planet.name);
        holder.tv_desc.setText(planet.desc);
    }

    /**
     * 得到总条数
     * @return
     */
    @Override
    public int getItemCount() {
        return planetList.size();
    }

    /**
     * 继承自己的MyViewHolder
     *获取单体布局中的每一个控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView iv_icon;
        TextView tv_name;
        TextView tv_desc;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_desc = itemView.findViewById(R.id.tv_desc);

        }
    }
}
